<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class User_profiles extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model(array('roles_model','users_model', 'user_profiles_model', 'classes_model'));
    }

    function index(){
        $data['users'] = $this->user_profiles_model->getAllUsers();
        $this->ingesup->render('user_profiles/index', $data);
    }

    function edit($user_id){
        if (isset($_POST['submit']) && isset($_POST['username'])){
            $res = true;
            /* MAJ user_profile */
            $user_profile_data = array(
                'firstname' => $_POST['firstname'],
                'lastname'  => $_POST['lastname']
            );
            if (!$this->user_profiles_model->save($user_profile_data,$user_id)){
                $res = false;
            }

            /* MAJ user */
            $user_data = array (
                'email'     => $_POST['email'],
                'role_id'   => $_POST['role']
            );
            if (!$this->users_model->save($user_data,$user_id)){
                $res = false;
            }
        }
        $data['user_info']      = $this->user_profiles_model->getUserInfos($user_id);
        $data['user_info']      = $data['user_info'][0];
        $data['user_roles']     = $this->roles_model->getAllRoles();
        $this->ingesup->render('user_profiles/edit', $data);
    }
    function view($user_id) {
        $data['user'] = $this->user_profiles_model->get_by_user($user_id);
        $this->ingesup->render('user_profiles/view', $data);
    }
}